package com.flyaudio.xiaopengautopilot.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.flyaudio.xiaopengautopilot.R;

public class SectorProgressView extends View {
    private int mProgressBgColor, mProgressContentColor;
    private Drawable mProgressCoverDrawable, mProgressThumbDrawable;

    private int mProgressCoverWidth, mProgressCoverHeight, mProgressCoverLeft, mProgressCoverTop;
    private int mDiameter, mProgress, mMax;
    private int mThumbWidth, mThumbHeight, mThumbLeft, mThumbTop, mThumbLimit_90, mThumbLimit_100;
    private int mTextColor, mTextSize, mTextX, mTextY;

    public SectorProgressView(Context context) {
        super(context);
    }

    public SectorProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (null != attrs) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FlySectorProgressObj);
            mProgressBgColor = typedArray.getColor(R.styleable.FlySectorProgressObj_mProgressBg, 0xff00ff00);
            mProgressContentColor = typedArray.getColor(R.styleable.FlySectorProgressObj_mProgressContent, 0xff0000ff);
            mProgressCoverDrawable = typedArray.getDrawable(R.styleable.FlySectorProgressObj_mProgressCover);
            mProgressCoverWidth = typedArray.getInteger(R.styleable.FlySectorProgressObj_mProgressCoverWidth, 0);
            mProgressCoverHeight = typedArray.getInteger(R.styleable.FlySectorProgressObj_mProgressCoverHeight, 0);
            mProgressCoverLeft = typedArray.getInteger(R.styleable.FlySectorProgressObj_mProgressCoverLeft, 0);
            mProgressCoverTop = typedArray.getInteger(R.styleable.FlySectorProgressObj_mProgressCoverTop, 0);
            mProgressThumbDrawable = typedArray.getDrawable(R.styleable.FlySectorProgressObj_mThumb);
            mThumbWidth = typedArray.getInteger(R.styleable.FlySectorProgressObj_mThumbWidth, 0);
            mThumbHeight = typedArray.getInteger(R.styleable.FlySectorProgressObj_mThumbHeight, 0);
            mThumbLeft = typedArray.getInteger(R.styleable.FlySectorProgressObj_mThumbLeft, 0);
            mThumbTop = typedArray.getInteger(R.styleable.FlySectorProgressObj_mThumbTop, 0);
            mThumbLimit_90 = typedArray.getInteger(R.styleable.FlySectorProgressObj_mThumbLimit_90, 0);
            mThumbLimit_100 = typedArray.getInteger(R.styleable.FlySectorProgressObj_mThumbLimit_100, 0);
            mDiameter = typedArray.getInteger(R.styleable.FlySectorProgressObj_mDiameter, 0);
            mProgress = typedArray.getInteger(R.styleable.FlySectorProgressObj_mProgress, 0);
            mMax = typedArray.getInteger(R.styleable.FlySectorProgressObj_mMax, 0);
            mTextColor = typedArray.getInteger(R.styleable.FlySectorProgressObj_mTextColor, 0xff00ff00);
            mTextSize = typedArray.getInteger(R.styleable.FlySectorProgressObj_mTextSize, 0);
            mTextX = typedArray.getInteger(R.styleable.FlySectorProgressObj_mTextX, 0);
            mTextY = typedArray.getInteger(R.styleable.FlySectorProgressObj_mTextY, 0);
            typedArray.recycle();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        drawProgressBg(canvas);
        drawProgressContent(canvas);
        drawProgressCover(canvas);
        drawProgressText(canvas);
        drawProgressThumb(canvas);
    }

    private void drawProgressBg(Canvas canvas) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(mProgressBgColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        RectF rectF = new RectF(0, 0, mDiameter, mDiameter);
        canvas.drawArc(rectF, (float) 124.5, (float) -69, true, paint);
    }

    private void drawProgressContent(Canvas canvas) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(mProgressContentColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        RectF rectF = new RectF(0, 0, mDiameter, mDiameter);
        float sweepAngle = (float) -0.69 * mProgress;
        canvas.drawArc(rectF, (float) 124.5, (float) sweepAngle, true, paint);
    }

    private void drawProgressCover(Canvas canvas) {
        Bitmap mProgressCoverBitmap = DrawableToBitmap(mProgressCoverDrawable, mProgressCoverWidth, mProgressCoverHeight);
        canvas.drawBitmap(mProgressCoverBitmap, mProgressCoverLeft, mProgressCoverTop, null);
    }

    private void drawProgressText(Canvas canvas) {
        Paint mPaint = new TextPaint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(mTextColor);
        mPaint.setTextSize(mTextSize);
        mPaint.setTextAlign(Paint.Align.CENTER);
        String progressText = mProgress + "%";
        canvas.drawText(progressText, mTextX, mTextY, mPaint);
    }

    private void drawProgressThumb(Canvas canvas) {
        Bitmap mProgressThumbBitmap = DrawableToBitmap(mProgressThumbDrawable, mThumbWidth, mThumbHeight);
        canvas.drawBitmap(mProgressThumbBitmap, mThumbLeft, mThumbTop, null);
    }

    private Bitmap DrawableToBitmap(Drawable drawable, int width, int height) {
        Bitmap bitmap = null;
        if (null != drawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            bitmap = bd.getBitmap();
            int src_w = bitmap.getWidth();
            int src_h = bitmap.getHeight();
            float scale_w = ((float) width) / src_w;
            float scale_h = ((float) height) / src_h;
            Matrix matrix = new Matrix();
            matrix.postScale(scale_w, scale_h);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix, true);
        }
        return bitmap;
    }

    public void setProgress(int progress) {
        if (progress > mMax) {
            mProgress = mMax;
        }else {
            mProgress = progress;
        }
        invalidate();
    }

    public int getProgress() {
        return mProgress;
    }

    public void setThumbLimitMode(boolean isFull) {
        if (isFull) {
            mThumbLeft = mThumbLimit_100 - mThumbWidth / 2;
        } else {
            mThumbLeft = mThumbLimit_90 - mThumbWidth / 2;
        }
        invalidate();
    }

}
